/**
 * Created by zxr on 2016/5/29.
 */
var express = require('express');
var router = express.Router();
var uglobal = require('../lib/global');
var mysql = require('../lib/mysql');
var muser = require('../lib/model/user');
var mteacher = require('../lib/model/teacher');
var events = require('events');


router.get('/',function(req,res,next){
    var user = uglobal.checkLogin(req);
    res.render('teacher/index',{title:uglobal.title,user:user});
});
router.get('/add',function(req,res,next){
    var user = uglobal.checkLogin(req);
    var quikling = req.query.quikling;
    if(!user){
        res.send('请登录后再试！<a href="/users/login">登录</a>');
    } else {
        res.render('teacher/add',{quikling:quikling,title:uglobal.title,user:user});
    }
});
var eventEmitter = new events.EventEmitter();
router.get('/list',function(req,res,next){
    var user = uglobal.checkLogin(req);
    var quikling = req.query.quikling;
        eventEmitter.removeAllListeners("uteacherlist");
        eventEmitter.removeAllListeners("nouteacher");
        eventEmitter.removeAllListeners("teacherlist");
        eventEmitter.removeAllListeners("noteacher");
    //先监听
        eventEmitter.on('noteacher',function(){
            res.render('teacher/list',{quikling:quikling,title:uglobal.title,user:user,teachers:[]});
        });
        eventEmitter.on('nouteacher',function(){
            res.render('teacher/list',{quikling:quikling,title:uglobal.title,user:user,teachers:[]});
        });


        eventEmitter.on('uteacherlist',function(uteachers){
            mysql.close();
            res.render('teacher/list',{quikling:quikling,title:uglobal.title,user:user,teachers:uteachers.teachers});
        });

        //获取老师   靶子 先摆靶子
        eventEmitter.on('teacherlist',function(ret){
            muser.getTUserByUids(ret.uids,ret.teachers,mysql,eventEmitter)
        });
        mysql.connect();
        mteacher.getTeachers(mysql,eventEmitter);
        //获取老师 和推荐uid   抢 后开枪


    //if(!user){
    //    res.send('请登录后再试！<a href="/users/login">登录</a>');
    //} else {
        //mysql.connect();
        //var sql = "select * from q_teacher";
        //mysql.query(sql,function(err,rows,fileds){
        //    var teachers = [];
        //    var uids = [];
        //    if(rows && rows.length > 0){
        //        for (var i = 0; i < rows.length; i++){
        //            var teacher = rows[i];
        //            teachers.push({
        //               "tid" : teacher.tid,
        //                "tname":teacher.tname,
        //                "pic":teacher.pic,
        //                "video":teacher.video,
        //                "intro":teacher.intro,
        //                "uid":teacher.uid
        //            });
        //            uids.push(teacher.uid);
        //        }
        //        var usql = "select * from q_user where uid in("+uids.join(",")+")";
        //        mysql.query(usql,function(uerr,urows,ufileds){
        //            if(rows && rows.length > 0){
        //                var users = {};
        //                for (var j = 0; j < urows.length; j++){
        //                    users[urows[j].uid] = urows[j].uname;
        //                }
        //                for (x in teachers){
        //                    teachers[x]['uname'] = users[teachers[x]['uid']];
        //                }
        //                res.render('teacher/list',{title:uglobal.title,user:user,teachers:teachers});
        //            }
        //
        //        });
        //
        //    }
        //
        //})

    //}
});
router.post('/add',function(req,res,next){
    var user = uglobal.checkLogin(req),
       uid = user.loginuid;
    if(!user){
        res.send('请登录后再试！<a href="/users/login">登录</a>');
    } else {
        console.log(uid);
        var info = req.body,
            tname = info.tname,
            pic = info.pic,
            intro = info.intro,
            video = info.video;

        mysql.connect();
        var sql = "INSERT INTO q_teacher (tname,pic,intro,video,uid) VALUE (?,?,?,?,?)";
        mysql.query(sql, [tname, pic, intro, video, uid], function (err, rows, fileds) {
            console.log(rows);
            console.log(uid);
            if (rows && rows.affectedRows == 1) {
                res.render('teacher/add', {title: uglobal.title, success: true, user: user});
            } else {
                res.render('teacher/add', {title: uglobal.title, errors: [{error: err}]});
            }
        })
    }
});


module.exports = router;